import 'package:flutter/material.dart';

///圆角搜索框显示
class SearchStaticBar extends StatelessWidget {
  ///搜索框上显示的文案
  final String hint;

  ///hero过渡动画的tag
  final String heroTag;

  ///搜索框的圆角
  final double defaultBorderRadius;

  ///外边距
  final EdgeInsets margin;

  ///内边距
  final EdgeInsets padding;

  ///点击的回调事件
  final Function? clickCallBack;

  ///如果考虑不需要水波纹效果那么就可以设置颜色为透明色
  final Color splashColor;

  ///边框背景色
  final Color? borderBgColor;

  ///搜索图标
  final Widget? searchIcon;

  ///提示文字的样式
  final TextStyle? hintTextStyle;

  const SearchStaticBar(
      {Key? key,
      required this.hint,
      this.defaultBorderRadius = 20.0,
      EdgeInsets? margin,
      EdgeInsets? padding,
      this.heroTag = '',
      this.clickCallBack,
      this.borderBgColor,
      this.splashColor = const Color(0xFFD6D6D6),
      this.searchIcon,
      this.hintTextStyle})
      : margin = margin ?? const EdgeInsets.only(top: 10.0, bottom: 10.0),
        padding = padding ?? const EdgeInsets.only(top: 8.0, bottom: 8.0),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      ///外边距
      margin: margin,
      child: Material(
        color: Colors.transparent,
        child: Ink(

            ///圆角边框
            decoration: BoxDecoration(
              color: borderBgColor ?? const Color(0xfff6f6f6),
              borderRadius: BorderRadius.circular(defaultBorderRadius),
            ),
            child: InkWell(
              onTap: () {
                if (clickCallBack != null) {
                  clickCallBack!();
                }
              },
              splashColor: splashColor,
              borderRadius: BorderRadius.circular(defaultBorderRadius),
              child: buildHeroWidget(context),
            )),
      ),
    );
  }

  ///构建[Hero]过渡动画
  Widget buildHeroWidget(BuildContext context) {
    if (heroTag.isEmpty) {
      return buildContainer(context);
    } else {
      return Hero(
        tag: heroTag,
        child: buildContainer(context),
      );
    }
  }

  ///构建搜索框的显示区域[Container]
  Container buildContainer(BuildContext context) {
    return Container(
      ///获取当前StatelessWidget的宽度
      width: MediaQuery.of(context).size.width,

      ///对齐方式
      alignment: const Alignment(-1, 0),

      ///内边距
      padding: padding,
      child: buildRow(),
    );
  }

  ///构建搜索图标与显示文本
  Row buildRow() {
    const String searchCommon = 'assets/images/search_common.png';
    return Row(
      ///居中
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        const Padding(padding: EdgeInsets.only(left: 10)),

       Expanded(
          child: Text(
            hint,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: hintTextStyle,
          ),
        ),
         searchIcon ??
            Image.asset(
              searchCommon,
              width: 16.0,
              height: 16.0,
            ),
        SizedBox(width:12)
      ],
    );
  }
}
